package com.moli.iov.base.remote.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.moli.iov.base.enums.YesOrNoEnum;
import com.moli.iov.base.model.dto.req.UpdateVehicleAttentionReqDto;
import com.moli.iov.base.model.dto.req.VehicleAttentionReqDto;
import com.moli.iov.base.model.po.VehicleAttention;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.remote.IVehicleAttentionRemoteService;
import com.moli.iov.base.service.IVehicleAttentionService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.JsonUtils;
import com.moli.iov.util.ValidationUtils;

import java.util.*;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆关注业务处理
 *
 * @author whl
 */
@Service(group = "${service.group}")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class IVehicleAttentionRemoteServiceImpl implements IVehicleAttentionRemoteService {

  @Autowired
  private IVehicleAttentionService attentionService;
  @Autowired
  private IVehicleBaseService iVehicleBaseService;

  @Override
  public RestResponse saveVehicleAttention(VehicleAttentionReqDto reqDto) {
    log.info("车辆监控 - 车辆管理-关注-添加-入参VehicleAttentionReqDto:{}", JsonUtils.toJson(reqDto));
    if (reqDto == null) {
       return   RestResponse.failure("添加车辆关注参数为空");
    }
    RestResponse restResponse = attentionService.saveVehicleAttention(reqDto);
    log.info("车辆监控 - 车辆管理-关注-添加-出参RestResponse:{}", JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse updateVehicleAttention(UpdateVehicleAttentionReqDto reqDto) {
    log.info("车辆监控 - 车辆管理-关注更新-入参UpdateVehicleAttentionReqDto:{}",JsonUtils.toJson(reqDto));
    if (reqDto == null) {
      return RestResponse.failure("车辆关注参数为空");
    }
    VehicleAttention vehicleAttentionByVin = attentionService
        .getVehicleAttentionByVin(reqDto.getVin(), reqDto.getSysUserId(), reqDto.getGroupId());
    if (vehicleAttentionByVin == null) {
      return RestResponse.failure("车辆关注不存在");
    }
    vehicleAttentionByVin.setOnlineReminder(reqDto.getOnlineReminder());
    vehicleAttentionByVin.setGmtModified(new Date());
    VehicleAttentionReqDto dto = new VehicleAttentionReqDto();
    CommonUtils.copyProperties(vehicleAttentionByVin, dto);
    RestResponse restResponse = attentionService.updateVehicleAttention(dto);
    log.info("车辆监控 - 车辆管理-关注更新-出参RestResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse saveBatchVehicleAttention(VehicleAttentionReqDto dto) {
    log.info("车辆监控 - 车辆管理-关注批量添加-入参VehicleAttentionReqDto:{}"+JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto,VehicleAttentionReqDto.Save.class);
    List<VehicleAttention> list = new ArrayList<>();
    if (CollectionUtils.isEmpty(dto.getVinList())) {
      throw new BusinessException("参数为空");
    }
    List<VehicleBase> vehicleBases = iVehicleBaseService.lisVehicleBaseByVin(dto.getVinList());
    List<String> collect = vehicleBases.stream().map(vehicleBase -> vehicleBase.getVin())
        .collect(Collectors.toList());
    if (CollectionUtils.isEmpty(vehicleBases)) {
      throw new BusinessException("参数为空");
    }

    for (String vin : dto.getVinList()) {
      if (collect.contains(vin)) {
        Date time = Calendar.getInstance().getTime();
        VehicleAttention attention = new VehicleAttention();
        attention.setVin(vin);
        attention.setUserId(dto.getSysUserId());
        attention.setGmtModified(time);
        attention.setGmtCreate(time);
        attention.setIsDelete(YesOrNoEnum.NO.getCode());
        attention.setCompanyId(dto.getCompanyId());
        attention.setCompanyName(dto.getCompanyName());
        attention.setGroupId(dto.getGroupId());
        attention.setOnlineReminder(dto.getOnlineReminder());
        list.add(attention);
      }
    }
    attentionService.saveBatchVehicleAttention(list, dto.getSysUserId(), dto.getGroupId());
    RestResponse success = RestResponse.success();
    log.info("车辆监控 - 车辆管理-关注批量添加-出参VehicleAttentionReqDto:{}"+JsonUtils.toJson(success));
    return success;
  }

  @Override
  public RestResponse deleteBatchVehicleAttention(VehicleAttentionReqDto dto) {
    log.info("车辆监控 - 车辆管理-关注批量删除-入参VehicleAttentionReqDto:{}"+JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto,VehicleAttentionReqDto.Delete.class);
    List<VehicleAttention>list = attentionService.listVehicleAttentionByVin(
            dto.getVinList(),dto.getSysUserId(), dto.getGroupId());
    if (CollectionUtils.isEmpty(list)) {
      throw new BusinessException("参数为空");
    }
    list.stream().forEach(vehicleAttention ->vehicleAttention.setIsDelete(YesOrNoEnum.YES.getCode()));
    attentionService.deleteBatchVehicleAttention(list, dto.getSysUserId(), dto.getGroupId());
    RestResponse success = RestResponse.success();
    log.info("车辆监控 - 车辆管理-关注批量删除-出参VehicleAttentionReqDto:{}"+JsonUtils.toJson(success));
    return success;
  }




}
